#!/usr/bin/env python3

import os
from safetensors import safe_open

# 基础信息
base_model_path = "models/Qwen1.5-1.8B"
finetuned_model_path = "training/models/finetuned_model_tianqi"
adapter_path = "training/models/finetuned_model_tianqi/adapter_model.safetensors"

print("=== 模型比较分析 ===")

# 基础模型信息
base_files = os.listdir(base_model_path)
print(f"基础模型文件: {base_files}")

# 微调模型信息
if os.path.exists(finetuned_model_path):
    finetuned_files = os.listdir(finetuned_model_path)
    print(f"微调模型文件: {sorted(finetuned_files)}")

# 适配器信息
if os.path.exists(adapter_path):
    with safe_open(adapter_path, framework="pt", device="cpu") as f:
        keys = list(f.keys())
        params = 0
        for key in keys:
            tensor = f.get_tensor(key)
            params += tensor.numel()
            
    print(f"适配器参数量: {params:,}")
    print(f"适配器矩阵数量: {len(keys)}")
    
    # 基础模型总参数量大约为18亿(1.8B)
    base_params = 1_800_000_000
    ratio = (params / base_params) * 100
    print(f"适配器参数占比: {ratio:.4f}%")
    
    print("\n适配器参数示例:")
    with safe_open(adapter_path, framework="pt", device="cpu") as f:
        for i, key in enumerate(keys[:4]):
            tensor = f.get_tensor(key)
            print(f"  {key}: {list(tensor.shape)}")
else:
    print("未找到适配器文件")

print("\n=== 分析完成 ===")
